<?php

class m_website_documents_WebsiteBase extends f_document_BaseDocument
{

	private $url;
	private $page = array();
	private $menus = array();
	private $displayLang;
	private $theme;

	public function __construct($id = null, $lang = null)
	{
		parent::__construct($id, null, $lang);
	}

	public function getTableName()
	{
		return "m_website__website";
	}

	public function getDocumentModel()
	{
		return "modules_website/website";
	}
	
	protected function getFieldsBag()
	{
		return array('document_id', 			'document_uid', 			'document_model', 			'document_author', 			'document_creationdate', 			'document_modificationdate', 			'document_publicationstatus', 			'document_lang', 			'document_revision', 			'document_startpublicationdate', 			'document_endpublicationdate', 			'document_label', 			'url', 			'page', 			'menus', 			'displayLang', 			'theme');
	}

	public function getdefaultPublicationstatus()
	{
		return "PUBLISHED";
	}

	/**
	 * @return boolean
	 */
	public function isRevisable()
	{
		return false;
	}
	
	/**
	 * @return boolean
	 */
	public function isLocalized()
	{
		return true;
	}	
	
    /**
	 * @return boolean
	 */
	public function isRecursive()
	{
		return $this->isRecursive;
	}
	
	/**
	 * @param boolean $value
	 * @return m_website_documents_WebsiteBase
	 */	
	public function setIsRecursive($value)
	{
		$this->isRecursive = $value;
	}
		
	/**
	 * @return string
	 */
	public function getUrl()
	{
		return $this->url;
	}

	/**
	 * @param string $value
	 * @return m_website_documents_Website
	 */
	public function setUrl($value)
	{
		$this->url = $value;
		return $this;
	}
	/**
	 * Add a new related document by document_uid	 
	 * 
	 * @return m_website_documents_Website
	 */
	public function addPageById($documentId, $orderNumber = null)
	{
		$document = f_document_Provider::getInstance()->getByUniqueId($documentId);
		return $this->addPage($document, $orderNumber);
	}

	/**
	 * Add a new related document
	 * 
	 * @return m_website_documents_Website
	 */
	public function addPage($document, $orderNumber = null)
	{		
		if(is_object($document) && $document instanceof m_website_documents_Page)
		{
			if(!is_array($this->page))
			{
				$this->page = array();
			}
		
			if(!in_array($document, $this->page))
			{
				if(is_null($orderNumber))
				{
					$this->page[] = $document;
				}
				else
				{
					$this->page[$orderNumber] = $document;
				}
			}
		}
		else
		{
			throw new Exception("Document is not a valid object or from valid model(m_website_documents_Page)");	
		}
		return $this;
	}
	
	/**
	 * Get the related documents in array
	 *
	 * @return array m_website_documents_Page
	 */
	public function getPage()
	{
		$relationManager = f_relation_Manager::getInstance();
		return $relationManager->getChilds($this, "page", $this->getLang());
	}
	
	/**
	 * Get related documents number
	 * 
	 * @return integer
	 */
	public function getPageNb()
	{
		return $this->page;
	}		
	
	/**
	 * Set the number of related documents
	 * Only used when saving or initialize document
	 */
	protected function setPage($value)
	{
		if(!is_null($value) && is_int($value))
		{
			$this->page = $value;
		}
		else
		{
			$dbProvider = f_database_Provider::getInstance();
	
			$sql = "SELECT COUNT(`child_id`) FROM `f_relation` WHERE `relation_type`=:relationType AND `lang`=:lang AND `parent_id`=:parentId";
			
			$nbRelations = $dbProvider->setQuery($sql)->execute(array("relationType" => 'page', "lang" => $this->getLang(), "parentId" => $this->getUniqueId()))->getQueryResult(PDO::FETCH_COLUMN);
	
			$this->page = intval($nbRelations);
		}
	}
	/**
	 * Add a new related document by document_uid	 
	 * 
	 * @return m_website_documents_Website
	 */
	public function addMenusById($documentId, $orderNumber = null)
	{
		$document = f_document_Provider::getInstance()->getByUniqueId($documentId);
		return $this->addMenus($document, $orderNumber);
	}

	/**
	 * Add a new related document
	 * 
	 * @return m_website_documents_Website
	 */
	public function addMenus($document, $orderNumber = null)
	{		
		if(is_object($document) && $document instanceof m_website_documents_Menu)
		{
			if(!is_array($this->menus))
			{
				$this->menus = array();
			}
		
			if(!in_array($document, $this->menus))
			{
				if(is_null($orderNumber))
				{
					$this->menus[] = $document;
				}
				else
				{
					$this->menus[$orderNumber] = $document;
				}
			}
		}
		else
		{
			throw new Exception("Document is not a valid object or from valid model(m_website_documents_Menu)");	
		}
		return $this;
	}
	
	/**
	 * Get the related documents in array
	 *
	 * @return array m_website_documents_Menu
	 */
	public function getMenus()
	{
		$relationManager = f_relation_Manager::getInstance();
		return $relationManager->getChilds($this, "menus", $this->getLang());
	}
	
	/**
	 * Get related documents number
	 * 
	 * @return integer
	 */
	public function getMenusNb()
	{
		return $this->menus;
	}		
	
	/**
	 * Set the number of related documents
	 * Only used when saving or initialize document
	 */
	protected function setMenus($value)
	{
		if(!is_null($value) && is_int($value))
		{
			$this->menus = $value;
		}
		else
		{
			$dbProvider = f_database_Provider::getInstance();
	
			$sql = "SELECT COUNT(`child_id`) FROM `f_relation` WHERE `relation_type`=:relationType AND `lang`=:lang AND `parent_id`=:parentId";
			
			$nbRelations = $dbProvider->setQuery($sql)->execute(array("relationType" => 'menus', "lang" => $this->getLang(), "parentId" => $this->getUniqueId()))->getQueryResult(PDO::FETCH_COLUMN);
	
			$this->menus = intval($nbRelations);
		}
	}
	/**
	 * @return string
	 */
	public function getDisplayLang()
	{
		return $this->displayLang;
	}

	/**
	 * @param string $value
	 * @return m_website_documents_Website
	 */
	public function setDisplayLang($value)
	{
		$this->displayLang = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getTheme()
	{
		return $this->theme;
	}

	/**
	 * @param string $value
	 * @return m_website_documents_Website
	 */
	public function setTheme($value)
	{
		$this->theme = $value;
		return $this;
	}
	public function relationFields()
	{
		return array_merge(array('page', 'menus'),parent::relationFields());
	}

	public function localizedFields()
	{
		return array(
                'document_id',
                'document_uid',
				'document_author', 
				'document_creationdate', 
				'document_modificationdate', 
				'document_lang', 
				'document_revision', 
				'document_label', 
				'document_publicationstatus', 'url', 'page', 'menus', 'displayLang', 'theme');
	}
	
	public function nonLocalizedFields()
	{
		return array(	'document_id', 			'document_uid', 			'document_model', 			'document_author', 			'document_creationdate', 			'document_modificationdate', 			'document_lang', 			'document_revision', 			'document_startpublicationdate', 			'document_endpublicationdate', 			'document_label');
	}
	
	/**
	 * Returns VO lang and translations langs
	 * @return array
	 */
	public function getAllLangs()
	{
		$langs = $this->getTranslationLangs();
		array_unshift($langs, $this->getVO());
		return $langs;
	}
	
	/**
	 * Returns all translations langs 
	 * @return array
	 */
	public function getTranslationLangs()
	{
		$dbProvider = f_database_Provider::getInstance();
		
		$sql = "SELECT `document_lang` FROM ".$this->getTableName()."_i18n WHERE `document_uid`=:id";
		
		return $dbProvider->setQuery($sql)->execute(array("id" => $this->getUniqueId()))->getQueryResults(PDO::FETCH_COLUMN);
	}
	
	/**
	 * Check if current document has at least one translation or a translation with the given lang
	 * @return boolean
	 */
	public function hasTranslation($lang = null)
	{
		if(is_null($lang))
		{
			return count($this->getTranslationLangs()) > 0;
		}
		else
		{
			return in_array(strtoupper($lang), $this->getTranslationLangs());
		}
	}

	/**
	 * Check if current document is translated or not
	 * @return boolean
	 */
	public function isTranslated()
	{
		return $this->getPublicationstatus() != parent::NOTRANSLATION;
	}

	/**
	 * Check if current document is a translation
	 * @return boolean
	 */
	public function isATranslation()
	{
		return $this->getLang() != $this->getVO();
	}
	
	/**
	 * Return the original document
	 * @return m_website_documents_Website
	 */
	public function getVoDocument()
	{
		if($this->isATranslation())
		{
			return m_website_documents_Website::getInstanceByUniqueId($this->getUniqueId(), $this->getVO());
		}
		return $this;
	}
	
	/**
	 * Return current document translation
	 * @return m_website_documents_Website
	 */	
	public function getTranslation($lang)
	{
		return m_website_documents_Website::getInstanceByUniqueId($this->getUniqueId(), $lang);
	}

}